package com.arthur.budget;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
    
    Button submitButton;
    Button undoButton;
    EditText spentInput;
    TextView totalView;
    
    BudgetManager bm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bm = BudgetManager.getInstance(this);
        
        submitButton = (Button) findViewById(R.id.submitButton);
        undoButton = (Button) findViewById(R.id.undoButton);
        spentInput = (EditText) findViewById(R.id.spentInput);
        totalView = (TextView) findViewById(R.id.totalView);
        
        submitButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                bm.spend(Float.parseFloat(spentInput.getText().toString()));
                updateTotal();
            }
        });

        undoButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                bm.undo();
                updateTotal();
            }
        });
    }
    
    @Override
    public void onResume() {
        super.onResume();
        updateTotal();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent intent = new Intent(this, Settings.class);
                startActivityForResult(intent, 0);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    private void updateTotal() {
        totalView.setText(String.valueOf(bm.getTotal()));
    }
}
